package com.easy.controller;

import com.easy.bean.Record;
import com.easy.bean.Station;
import com.easy.common.CommonResult;
import com.easy.service.IStationService;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
public class StationController {

    @Autowired
    IStationService stationService;

    //通过ID获取
    @GetMapping("station/{id}")
    public Station getStationByID(@PathVariable int id) {
        return stationService.getStationByID(id);
    }

    //获取列表
    @GetMapping("station")
    @Transactional
    public CommonResult getStation(Station station, Page page) {
        List<Station> list =stationService.getStation(station,page);
        int count=stationService.getCount(station);
        HashMap map=new HashMap();
        map.put("list",list);
        map.put("count",count);
        return CommonResult.success(map);
    }

    //删除
    @DeleteMapping("station/{id}")
    public CommonResult delStation(@PathVariable int id) {
        int count = stationService.delStation(id);
        if(count>0){
            return CommonResult.success(count);
        }else {
            return CommonResult.fail();
        }

    }

    //新增JSON
    @PostMapping("stationjson")
    @Transactional
    public CommonResult addStationJSON(@RequestBody Station station){
        stationService.addStation(station);
        station = stationService.getStationByID(station.getId());
        return CommonResult.success(station);
    }

    //修改
    @PutMapping("stationjson")
    @Transactional
    public CommonResult editStationJSON(@RequestBody Station station) {
        stationService.editStation(station);
        station = stationService.getStationByID(station.getId());
        return CommonResult.success(station);
    }
}
